package BLL;

import UI.Main.SearchPage;

import javax.swing.*;
import java.io.File;
import java.util.*;


/**
 * 搜索功能
 */
public class FileSearch {
    /**
     * 搜索函数，使用BFS进行搜索
     * @param path 搜索路径，只搜索在该路径下的文件
     * @param regex 待搜索文件/目录名（模糊搜索）
     * @param searchDir 是否搜索目录
     * @param searchFile 是否搜索文件
     */
    public static void bfsFile(String path, String regex, boolean searchDir, boolean searchFile)
    {
        boolean foundAny = false;
        regex = "(.*)" + regex + "(.*)";
        Queue<File> queue = new LinkedList<>();
        Vector<File> fileVector = new Vector<>();
        File[] diskFiles;

        System.out.println("StartSearch: " + path);

        if(path.equals(""))
        {
            List<String> Disks = MemoryInfo.getDisk();
            for (String disk : Disks)
            {
                path = disk + '\\';
                diskFiles = new File(path).listFiles();
                if (diskFiles != null)
                {
                    Collections.addAll(fileVector, diskFiles);
                }
            }
        }
        else
        {
            diskFiles = new File(path).listFiles();
            if (diskFiles != null)
            {
                Collections.addAll(fileVector, diskFiles);
            }
        }

        for(File f : fileVector)
        {
            if(f.isDirectory())
                queue.offer(f);
            else if(searchFile && f.getName().matches(regex))
            {
                foundAny = true;
                SearchPage.mapSearch.put(f.getName(), f.getAbsolutePath());
                SearchPage._instance.defaultListModel.addElement(f.getName());
                SearchPage._instance.icons[SearchPage._instance.iconCounter] = FileIcon.getIcon(f.getAbsolutePath());
                ++SearchPage._instance.iconCounter;
            }
        }

        while (!queue.isEmpty())
        {
            File tFile = queue.poll();

            System.out.println("Search: " + tFile);

            if(searchDir)
            {
                if(tFile.getName().matches(regex))
                {
                    foundAny = true;
                    SearchPage.mapSearch.put(tFile.getName(), tFile.getAbsolutePath());
                    SearchPage._instance.defaultListModel.addElement(tFile.getName());
                    SearchPage._instance.icons[SearchPage._instance.iconCounter] = FileIcon.getIcon(tFile.getAbsolutePath());
                    ++SearchPage._instance.iconCounter;
                }
            }
            
            File[] tempFileArray = tFile.listFiles();
            if(tempFileArray == null)
                continue;
            for(File f : tempFileArray)
            {
                if(f.isDirectory())
                    queue.offer(f);
                else
                {
                    if(searchFile && f.getName().matches(regex))
                    {
                        foundAny = true;
                        SearchPage.mapSearch.put(f.getName(), f.getAbsolutePath());
                        SearchPage._instance.defaultListModel.addElement(f.getName());
                        SearchPage._instance.icons[SearchPage._instance.iconCounter] = FileIcon.getIcon(f.getAbsolutePath());
                        ++SearchPage._instance.iconCounter;
                    }
                }
            }
        }
        if(!foundAny)
        {
            JOptionPane.showMessageDialog(null, "未找到任何结果", "搜索结果", JOptionPane.YES_OPTION);
        }
    }
}
